package com.czy.owner.ui.order;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.czy.owner.R;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.ChainStoresModel;
import com.czy.owner.entity.WebViewRequestParam;
import com.czy.owner.global.Constants;
import com.czy.owner.ui.BaseActivity;
import com.czy.owner.ui.LuckDrawWebviewActivity;
import com.czy.owner.ui.login.LoginActivity;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.SharedPreferencesUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

//显示静态页的界面
public class ProjectDetailsWebviewActivity extends BaseActivity {

    @BindView(R.id.webview)
    WebView webview;
    @BindView(R.id.myProgressBar)
    ProgressBar myProgressBar;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    private WebViewRequestParam webViewRequestParam;
    private int myType = 0;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        myProgressBar.setVisibility(View.GONE);
        if (getIntent().getExtras() == null) {
            Toast.makeText(this, "数据异常", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        webViewRequestParam = (WebViewRequestParam) getIntent().getSerializableExtra(Constants.TRANSMISSION_CONTENT_KEY);
        if (webViewRequestParam != null) {
            toolbarTitle.setText(webViewRequestParam.getTitle());
        }
        webview.setWebViewClient(new MyWebViewClient());
        WebSettings ws = webview.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setUseWideViewPort(false);
        ws.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        ws.setDomStorageEnabled(true);

        if (webViewRequestParam.getRequestMethod() == WebViewRequestParam.REQUEST_POST) {
            webview.postUrl(webViewRequestParam.getUrl(), webViewRequestParam.getParams().getBytes());

        } else {
            MyLog.e("yang", "webViewRequestParam==" + webViewRequestParam.getUrl());
            webview.loadUrl(webViewRequestParam.getUrl() + "?" + webViewRequestParam.getParams().getBytes());
        }

//        http://118.178.188.69/own/shop/priceRatio/info?infoId=2686
//        http://118.178.188.69/pub/goodsView?goodsId=1053

        // http://118.178.188.69/own/shop/priceRatio?joinType=0&goodsActivityId=339  立即参与
        //joinType=0 商品列表  joinType=1 热门商城分类

        webview.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                MyLog.e("yang--", url);

                if (url.toString().contains("storeServiceId=")) {
                    int storeId = Integer.parseInt(SharedPreferencesUtils.getParam(ProjectDetailsWebviewActivity.this, UserHelper.getInstance().getUserAccount() + Constants.SHARED_PREFERENCES_KEY_BINDSTOREID, 0) + "");
                    ChainStoresModel chainStores = new ChainStoresModel();
                    chainStores.setStoreId(storeId);
                    startActivity(new Intent(ProjectDetailsWebviewActivity.this, SelectBespokeActivity.class).putExtra("isClose", true).putExtra("chainStores", chainStores));
                    return true;
                }
                return super.shouldOverrideUrlLoading(view, url);
            }

//            @Override
//            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
//                MyLog.e("yang--", request.getUrl().toString());
//                String mPattern = "1212";
//                if (request.getUrl().toString().contains("own/login")) {
//                    startActivity(new Intent(LuckDrawWebviewActivity.this, LoginActivity.class).putExtra(Constants.TRANSMISSION_CONTENT_KEY, false));
//
//                }else if (request.getUrl().toString().contains("/own/share")){
//                    getShare();
//                    return null;
//                }
//                return super.shouldInterceptRequest(view, request);
//            }


        });
//        webview.setWebViewClient(new WebViewClient() {
//            @Override
//            public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
//
//
//                return super.shouldInterceptRequest(view, request);
//            }
//
//
//        });
        webview.setWebChromeClient(new WebChromeClient() {

            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                // TODO Auto-generated method stub
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    if (myProgressBar != null) {
                        myProgressBar.setVisibility(View.GONE);
                    }
                    if (myProgressBar != null) {
                        myProgressBar.setVisibility(View.GONE);
                    }
                } else {
                    myProgressBar.setVisibility(View.VISIBLE);
                    myProgressBar.setProgress(newProgress);

                }
            }

        });

    }

    @Override
    protected void updateViews() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }


    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (webViewRequestParam.getRequestMethod() == WebViewRequestParam.REQUEST_POST) {
                view.postUrl(url, webViewRequestParam.getParams().getBytes());
            } else {
                view.loadUrl(url + "?" + webViewRequestParam.getParams().getBytes());
            }

            return true;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            getToolbarTitle().setText(view.getTitle());
            mLoadView.CancleLoadView();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            getToolbarTitle().setText("");
            mLoadView.ShowLoadView();
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webview.destroy();
    }
}
